package com.ruoyi.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.UserMapper;
import com.ruoyi.domain.User;
import com.ruoyi.service.IUserService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学员Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-14
 */
@Service
@Transactional
public class UserServiceImpl implements IUserService 
{
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询学员
     * 
     * @param userId 学员主键
     * @return 学员
     */
    @Override
    public User selectUserByUserId(Long userId)
    {
        return userMapper.selectUserByUserId(userId);
    }

    /**
     * 查询学员列表
     * 
     * @param user 学员
     * @return 学员
     */
    @Override
    public List<User> selectUserList(User user)
    {
        return userMapper.selectUserList(user);
    }

    /**
     * 新增学员
     * 
     * @param user 学员
     * @return 结果
     */
    @Override
    public int insertUser(User user)
    {
        return userMapper.insertUser(user);
    }

    /**
     * 修改学员
     * 
     * @param user 学员
     * @return 结果
     */
    @Override
    public int updateUser(User user)
    {
        return userMapper.updateUser(user);
    }

    /**
     * 批量删除学员
     * 
     * @param userIds 需要删除的学员主键
     * @return 结果
     */
    @Override
    public int deleteUserByUserIds(Long[] userIds)
    {
        return userMapper.deleteUserByUserIds(userIds);
    }

    /**
     * 删除学员信息
     * 
     * @param userId 学员主键
     * @return 结果
     */
    @Override
    public int deleteUserByUserId(Long userId)
    {
        return userMapper.deleteUserByUserId(userId);
    }

    @Override
    public User selectUserByUserName(String userName) {
        return userMapper.selectUserByUserName(userName);
    }

    @Override
    public void recharge(Integer num, String username) {
        User user = userMapper.selectUserByUserName(username);
        User user1 = new User();
        user1.setUserId(user.getUserId());
        user1.setUserAccount(String.valueOf(Integer.valueOf(user.getUserAccount()) + num));
        userMapper.updateUser(user1);
    }

    @Override
    public void pay(Integer num, String username) {
        User user = userMapper.selectUserByUserName(username);
        Integer userAccount = Integer.valueOf(user.getUserAccount());
        if (userAccount < num) {
            throw new RuntimeException("余额不足");
        }
        User user1 = new User();
        user1.setUserId(user.getUserId());
        user1.setUserAccount(String.valueOf(userAccount  - num));
        userMapper.updateUser(user1);
    }
}
